package com.yuqiangz.juc;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @author 赵雨强
 * @create 2021/7/11 10:04
 * @description CountDownLatch的使用示例
 */
public class CountDownLatchDemo {
    public static void main(String[] args) {
        int cut = 6;
        CountDownLatch countDownLatch = new CountDownLatch(cut);

        for (int i = 0; i < 6; i++) {
            int finalI = i;
            new Thread(() ->{
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("同学" + finalI + "离开了班级");
                countDownLatch.countDown();
            },String.valueOf(i)).start();
        }

        new Thread(()->{
            System.out.println("班长要锁门啦");
            try {
                countDownLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("同学都走了，班长把门锁了");
        }).start();

        System.out.println("主线程结束");
    }
}
